
<script type="text/javascript">
	function reloadInvoice(){
		$("#tblInvoice").jqGrid("setGridParam",{postData:{customer_id:$("#customer_id").val()},datatype:"json"}).trigger("reloadGrid");
		
	}

	function calculateTotal(){
		selrow = $("#tblInvoice").jqGrid("getGridParam","selarrrow");
		total =0;
		payment =0;
		for(i=0;i<selrow.length;i++){
			rowid = selrow[i];
			rowData = $("#tblInvoice").jqGrid("getLocalRow",rowid);
			//ttlInvoice = rowData.ttl_invoice * 1 ;
			//ttlDisc = rowData.ttl_disc * 1 ;
			
			total += rowData.total * 1;
			payment += rowData.payment * 1;
			
		}
		$("#ttlInvoice").val(formatMoney(total));
		$("#ttl_payment").val(formatMoney(payment));
	}

	function save(){		
		$("#tblInvoice").jqGrid('saveCell',lastIRow,lastICol);
		arrSelected = $("#tblInvoice").jqGrid("getGridParam","selarrrow");
		selectedData = new Array();
		for (i=0;i<arrSelected.length;i++){
			data = $("#tblInvoice").jqGrid("getRowData",arrSelected[i]);
			selectedData.push(data);		
		}
		$("#arrPaidInvoice").val(JSON.stringify(selectedData));	
		$("#frmData").submit();
			
	}
	var lastIRow =0;
	var lastICol =0;
	$(function(){
		$("#customer").autocomplete({
			source:<?php echo $acCustomer;?>,
			change:function(event,ui){
				if(ui.item == null){
					alert("Invalid Customer!");
					$("#customer_id").val("");
					$("#customer").val("");
					$("#customer").focus();
				}else{
					
					if ($("#customer_id").val() != ui.item.id){
						$("#customer_id").val(ui.item.id);
						reloadInvoice();
					}
				}
			},
			select:function(event,ui){
				if(ui.item == null){
					alert("Invalid Customer!");
					$("#customer_id").val("");
					$("#customer").val("");
					$("#customer").focus();
				}else{
					if ($("#customer_id").val() != ui.item.id){
						$("#customer_id").val(ui.item.id);
						reloadInvoice();
					}
				}
			}
		});

		$("#tblInvoice").jqGrid({
			url:'index.php?r=keuangan/tu/customerPayment/invoiceList',
			postData:{},
			datatype: 'local',
			data:<?php echo $arrInvoice; ?>,
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			multiselect:true,
			colNames: ['id','Invoice No','Date','Total','Payment','Notes'],
			colModel: [
				{name: 'id', index: 'id', width: 20,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'invoice_no', index: 'invoice_no', width: 40,search:true},
				{name: 'invoice_date', index: 'invoice_date', width: 40,search:true,template:dateTemplate},				
				{name: 'total', index: 'total', width: 40,template:moneyTemplate},
				{name: 'payment', index: 'payment', width: 40,template:moneyTemplate,editable:true},
				{name: 'notes', index: 'notes',width:100,editable:true},
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblInvoice',
			viewrecords: true,
			caption: '',
			cellEdit:true,
			cellsubmit:"clientArray",
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			onSelectRow:function (rowId,status,e){
				calculateTotal();
			},
			onSelectAll:function(arrRowId,status){
				calculateTotal();
			},
			afterEditCell:function(rowid, celname, value, iRow, iCol){
				lastIRow = iRow
				lastICol = iCol
			},
			afterSaveCell:function(rowid, cellname, value, iRow, iCol){
				calculateTotal();
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblInvoice").getDataIDs();				 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i];
					row = $("#tblInvoice").jqGrid("getLocalRow",cl);  
					btnPrint = "<img title='Print' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-document-print-icon.png' onclick='printInvoice(" + cl + ")' />";					 
					//jQuery("#tblInvoice").setRowData(ids[i],{total:total})
					
					if (row.id == <?php echo $invoice_id?>){
						$("#tblInvoice").jqGrid("setSelection",row.id);
						//alert(row.invoice_no);
					} 
				} 
			}                     		
				
		});		
	});
	
	function cancel(){
		 
		 //var ttlRow = $("#tblInvoice").jqGrid('getGridParam', 'records');
		 //$("#tblInvoice").jqGrid('saveCell',1,7);
		 $("#tblInvoice").jqGrid('saveCell',lastIRow,lastICol);	 
		 
		 //for(idIndex = 1; idIndex <= ttlRow; ++idIndex){
			// $("#tblInvoice").jqGrid('saveCell',idIndex,7);	 
		 //}
		 
		 /*
		var rowIds = $('#tblInvoice').jqGrid('getDataIDs');
		
		for(idIndex = 0; idIndex < rowIds.length; ++idIndex){
		     $("#tblInvoice").jqGrid('saveRow',rowIds[idIndex]); 
		}
			*/
	}
</script>
<div>
	<div style="border-bottom:2px solid #000;padding-bottom:5px;margin-bottom:20px">
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => "Simpan",
						'icon' => "images/action/Actions-document-save-all-icon.png",
						'callback'=>"save"
					),					
					array(
						'id' => "btnCancel",
						'title' => "Batal",
						'icon' => "images/action/Actions-dialog-cancel-icon.png",
						'callback'=>"cancel"
					),
				)
			)
		);
	?>
	</div>
	<?php
		$labelWidth = "100px"; 
	?>
	<form id="frmData" action="index.php?r=keuangan/tu/customerPayment/edit" method="POST" target="_blank">
		<div>
			<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>">Payment NO :</label>
			<input id="payment_no" name="payment_no" type="text" value="<?php echo $payment_no;?>" readonly>
			<input id="id" name="id" type="hidden" value="<?php echo $id;?>" readonly>
			<input id="op" name="op" type="hidden" value="<?php echo $op;?>" readonly>
		</div>
		
		<div>
			<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>">Account Receive :</label>
			<select id="receive_acc_id" name="receive_acc_id" >
				<?php
					foreach($arrReceiveAcc as $receiveAcc){	
						echo "<option value='$receiveAcc->id'>".$receiveAcc->acc_code ." - ". $receiveAcc->name."</option>";
					} 
				?>
			</select>
		</div>
		
		<div>
			<label class="form mandatory" style="text-align:right;width:<?php echo $labelWidth;?>">Date :</label>
			<?php
				$this->widget('application.components.widgets.DateTextField', array('name'=>'payment_date','value'=>$payment_date));
			?>
		</div>
		
		<div>
			<label class="form mandatory" style="text-align:right;width:<?php echo $labelWidth;?>">Customer :</label>
			<input id="customer" name="customer" type="text" style="width:300px" value="<?php echo $customer_name; ?>">
			<input id="customer_id" name="customer_id" type="hidden" value="<?php echo $customer_id; ?>">
		</div>
		<div>
			<label class="form mandatory" style="vertical-align:top;text-align:right;width:<?php echo $labelWidth;?>">Notes :</label>
			<textarea id="notes" name="notes" style="width:300px;height:50px"></textarea>
		</div>
		
		<div style="height:20px"></div>
		<table id="tblInvoice"></table>
		<div id="navTblInvoice"></div>	
		<div style="width:100%;text-align:right;font-size:16px;font-weight:bold;display:none">
			<label>Total : </label> 
			<input type="text"   id="ttlInvoice" name="ttlInvoice" style="width:100px;text-align:right;font-size:16px;font-weight:bold" value="0" readonly  />
		</div>
			
		<div style="width:100%;text-align:right;font-size:16px;font-weight:bold">
			<label>Total Payment:</label> 
			<input type="text" class="currency" id="ttl_payment" name="ttl_payment" style="width:100px;text-align:right;font-size:16px;font-weight:bold" value="0" readonly/>
		</div>
		
		<textarea style="display:inline" id="arrPaidInvoice" name="arrPaidInvoice"></textarea>
	</form>
</div>